import { defineStore } from 'pinia'
import {
  reqGategpry1,
  reqGetGategpry,
  reqGategpry2,
  reqGategpry3,
} from '@/api/product/attr/index.ts'
import {reqHasSpu} from "@/api/product/spu";
import { ref } from 'vue'
export const useCategorystore = defineStore('Category', () => {
  const Category1 = ref()
  const Category2 = ref()
  const Category3 = ref()
  const ID1 = ref()
  const ID2 = ref()
  const ID3 = ref()
  const CategoryList = ref()
  const spuList = ref()
  const sputotal = ref()
  const loading = ref(false)
  const isadd = ref(false)

  const setspuList = (list:any)=>{
    spuList.value = list
  }
  const reqCategory1 = async () => {
    const p = await reqGategpry1()
    Category1.value = p.data
  }
  const reqCategory2 = async (id: number) => {
    const p = await reqGategpry2(id)
    Category2.value = []
    Category3.value = []
    CategoryList.value = []
    isadd.value = false
    Category2.value = p.data
  }
  const reqCategory3 = async (id: number) => {
    const p = await reqGategpry3(id)
    Category3.value = []
    CategoryList.value = []
    isadd.value = false
    Category3.value = p.data
  }
  const reqCategoryList = async (id1: number, id2: number, id3: number,type:String) => {
    loading.value = true
    ID3.value = id3
    ID2.value = id2
    ID1.value = id1
    if(type === 'attr'){
      try {
        const p = await reqGetGategpry(id1, id2, id3)
        loading.value = false
        CategoryList.value = p.data
        isadd.value = true
      } catch (e) {
        loading.value = false
      }
    }else if(type === 'spu'){
      const p = await reqHasSpu(1,3,id3)
      spuList.value = p.data.records
      sputotal.value = p.data.total
      console.log(p)
    }

  }
  const resetState = () => {
    Category1.value = null
    Category2.value = null
    Category3.value = null
    CategoryList.value = null
    ID1.value = null
    ID2.value = null
    ID3.value = null
    loading.value = false
    isadd.value = false
    sputotal.value = null
    spuList.value = null
  }

  return {
    Category1,
    Category2,
    Category3,
    CategoryList,
    reqCategory1,
    reqCategory2,
    reqCategory3,
    reqCategoryList,
    spuList,
    sputotal,
    loading,
    isadd,
    ID3,
    ID2,
    ID1,
    resetState,
    setspuList
  }
})
